package Node;

import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;
import org.apache.thrift.transport.TTransportException;
import org.ini4j.Ini;

import HBFailureService.HBFailureService;
import HBFailureService.HBFailureService.Processor;
import HBFailureService.HBHandler;

/**
 * Class: Server
 *
 */
public class Server implements Runnable {

    // Member variables
	private TServer myServer;
	private TMultiplexedProcessor processor;
	
	/**
	 * Constructor for Server object
	 * 
	 * @param listenPort the port to listen on
	 * @throws TTransportException
	 */
	//public Server (int listenPort, Ini serviceConfig) throws TTransportException {    
	public Server (int listenPort) throws TTransportException {  
	    // Create multiplexed processor (one server multiple services)
		
		
	    processor = new TMultiplexedProcessor();
	    
	    // Setup transport layer
	    TServerSocket t = new TServerSocket(listenPort);
	    myServer = new TThreadPoolServer(new TThreadPoolServer.Args(t).processor(processor));
	}
	
	/**
	 * Function to register a handler with the server object
	 * 
	 * @param handlerName the name of the handler
	 * @param handler the handler to register with the server
	 */
	public void registerHandler(String handlerName, TProcessor handler) {
	    ((TMultiplexedProcessor) processor).registerProcessor(handlerName, handler);
	}
	
	/**
	 * Entry point for starting the thread
	 */
	public void run() {
	    // Start the server
	    try {
	        myServer.serve();
	    } 
	    catch (Exception e) {
	        e.printStackTrace();
	    }
	}
}
